package com.fanh.common.util;

import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yaofeng
 * @Description: 短信发送工具类
 * @Date: 2021/3/18 下午9:13
 * @Version: 1.0
 */
public class SmsCodeUtil {
    public static final String SEND_URL = "api.sms.ronghub.com/sendCode.json";
    public static final String VERIFY_URL = "api.sms.ronghub.com/verifyCode.json";

    public static final String APP_KEY = "82hegw5u81nix";
    public static final String TIME_STAMP = "1616074656115";
    public static final String NONCE = "13579";
    public static final String APP_SECRET ="zQbsap0PLH";

    public static JSONObject sendSms(String phone) {
        Map<String, Object> params = new HashMap<>();
        params.put("mobile", phone);
        params.put("templateId", "6wLZl_LpkPKbfy8_pkro7e");
        params.put("region", "86");
        return post(SEND_URL, params);
    }


    public static JSONObject verifySms(String sessionId, String code) {
        Map<String, Object> params = new HashMap<>();
        params.put("sessionId", sessionId);
        params.put("code", code);

        return post(VERIFY_URL, params);
    }


    private static JSONObject post(String url, Map<String, Object> params) {
        String bytes = DigestUtil.sha1Hex(APP_SECRET+NONCE+TIME_STAMP);

        String result3 = HttpRequest.post(url)
                .header("App-Key", APP_KEY)//头信息，多个头信息多次调用此方法即可
                .header("Nonce", NONCE)
                .header("Timestamp", TIME_STAMP)
                .header("Signature ", bytes)
                .form(params)//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();
         return JSONObject.parseObject(result3);
    }

    public static void main(String[] args) {
        //  JSONObject jsosn = sendSms("15388010913");
        // System.out.println(jsosn);
       // {"code":200,"sessionId":"09-A0D48FA5-261D-23E2-BF0F-9A41DBAE62ED"}

        JSONObject jsosn =  verifySms("09-82C7454F-46EA-115D-AF5C-AAE23C9FCE35","325480");
        System.out.println(jsosn);
    }


}
